/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.model;

import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * A class representing the state of the model.
 * @author	Fonseca, Flavio & Pitangueira, Roque
 * @see     gui.Interface
 * @see     gui.DrawingArea
 * @since   January 2004
 */
public class ModelState implements Serializable {
	
	private static final long serialVersionUID = 1L;
	private String name = new String("untitled.isn");
    private transient String path = new String();
    
	public transient static final int SHAPES = 1;
	public transient static final int SUBREGIONS = 2;
	public transient static final int PARTITION = 3;
	public transient static final int GENERATION = 4;
	public transient static final int NODAL_ATTRIBUTES = 5;
	public transient static final int ELM_ATTRIBUTES = 6;
	public transient static final int PROC_INTERACTIVE = 7;
	public transient static final int PROC_AUTOMATIC = 8;
	public transient static final int REACTIONS = 9;
	public transient static final int DIAGRAMS = 10;
	public transient static final int DEFORM = 11;
	public transient static final int REPORT = 12;
	
	// The colors of the drawing model
    private Color bgcolor = Color.BLACK;
    public Color gridColor = Color.GRAY;
    
	public Color pointColor = Color.RED;
	
	public Color controlPointColor = Color.BLUE;
	public Color previewElementColor = Color.BLUE;
    
	public Color lineColor = Color.YELLOW;
    
	public Color regionColor = new Color(255, 255, 220);
	public Color curveColor = Color.MAGENTA;
	public Color parelementColor = new Color(255, 255, 200);
	public Color boundaryColor = Color.BLUE;
	
	public Color boundaryElementColor = Color.YELLOW;
    
	public Color markColor = Color.ORANGE;
	public Color divisionNodeColor = Color.RED;
	
	public Color supportColor = Color.GREEN;
	public Color springColor = Color.GREEN;
    public Color loadColor = Color.GREEN;
    public Color distributedLoadColor = Color.GREEN;
	
    public Color concForcePointColor = new Color(153, 0, 0);
	public Color displaceColor = new Color (153, 102, 0);
    public Color preStrainsColor = new Color(0, 102, 51);
	public Color iniEqForceColor = new Color(255, 102, 0);
	public Color reactionsColor = Color.RED;
	public Color tempChangeColor = Color.RED;
	
	public Color globalAxisColor = Color.WHITE;
	
	public Color localAxisColor = Color.MAGENTA;
	public Color deformColor = new Color(102, 102, 0);
	public Color fxDiagramColor = Color.RED;
	public Color fyDiagramColor = new Color(0, 102, 51);
	public Color fzDiagramColor = Color.PINK;
	public Color mxDiagramColor = Color.ORANGE;
	public Color myDiagramColor = Color.MAGENTA;
	public Color mzDiagramColor = Color.BLUE;
	public Color dxDiagramColor = new Color(102, 102, 0);
	public Color dyDiagramColor = new Color(102, 102, 0);
	public Color dzDiagramColor = new Color(102, 102, 0);
	public Color rxDiagramColor = new Color(102, 102, 0);
	public Color ryDiagramColor = new Color(102, 102, 0);
	public Color rzDiagramColor = new Color(102, 102, 0);
	
    // The limits of drawing model
    private double x = 0;
    private double y = 0;
    private double width = 10;
    private double height = 10;
    
    // The Current zoom of drawing model
    private double zx = 0;
    private double zy = 0;
    private double zwidth = 10;
    private double zheight = 10;
	
    //The grid increments
    private double xInc = 1;
    private double yInc = 1;
    
    //The scales
    public double loadScale = 1;
    public double supportScale = 1;
    public double nodeScale = 4;
	public double displaceScale = 1;
    public double deformScale = 1;
    public double diagramScale = 1;
	public double concForcePointScale = 4;
	public double preStrainsScale = 0.5;
	public double iniEqForceScale = 1;
	public double reactionsScale = 1;
	
    private boolean snap = true;
    private boolean grid = true;
    
	private boolean viewGlobalAxis = true;
	private boolean viewLocalAxis = false;
	private boolean viewNodeLabel = true;
	private boolean viewElementLabel = true;
	private boolean viewRegionLabel = true;
	
	public boolean viewFxDiagram = false;
	public boolean viewFyDiagram = false;
	public boolean viewFzDiagram = false;
	public boolean viewMxDiagram = false;
	public boolean viewMyDiagram = false;
	public boolean viewMzDiagram = true;
	
	public boolean viewDxDiagram = false;
	public boolean viewDyDiagram = false;
	public boolean viewDzDiagram = false;
	public boolean viewRxDiagram = false;
	public boolean viewRyDiagram = false;
	public boolean viewRzDiagram = false;
	
	private int actualTreeNode = 0;
	
	private ArrayList<Color> colors = new ArrayList<Color>();
	
    private int precision = 2;
	
//*****************************************************************************
    
    public ModelState() {
    }
    
//*****************************************************************************
	
    public void setName(String s) {
        String ext = new String();
        int i = s.lastIndexOf('.');
        if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
        }
        if (!ext.equals("isn")){
                s = s + ".isn";
        }
        name = s;
    }
    
//*****************************************************************************
	
    public String getName() {
        return name;
    }
    
//*****************************************************************************
	
    public void setPath(String s) {
        path = s;
    }
    
//*****************************************************************************
	
    public String getPath() {
        return path;
    }
    
//*****************************************************************************
	
    public void setLimits(Rectangle2D lim) {
        Rectangle2D.Double limits = new Rectangle2D.Double();
        limits.setRect(lim);
        x = limits.x;
        y = limits.y;
        width = limits.width;
        height = limits.height;
    }
    
//*****************************************************************************
	
    public Rectangle2D.Double getLimits() {
        Rectangle2D.Double limits = new Rectangle2D.Double();
        limits.x = x;
        limits.y = y;
        limits.width = width;
        limits.height = height;
		return limits;
    }
    
//*****************************************************************************
    
    public void setCurrentZoom(Rectangle2D lim) {
        Rectangle2D.Double limits = new Rectangle2D.Double();
        limits.setRect(lim);
        zx = limits.x;
        zy = limits.y;
        zwidth = limits.width;
        zheight = limits.height;
    }
    
//*****************************************************************************
	
    public Rectangle2D.Double getCurrentZoom() {
        Rectangle2D.Double limits = new Rectangle2D.Double();
        limits.x = zx;
        limits.y = zy;
        limits.width = zwidth;
        limits.height = zheight;
        return limits;
    }
	
//*****************************************************************************
	
    public void setPrecision(int i) {
        precision = i;
    }
    
//*****************************************************************************
	
    public int getPrecision() {
        return precision;
    }
    
//*****************************************************************************
	
    /**
     * Sets the Background color.
     * @param c The desired color to be set.
     */
    public void setBackground(Color c) {
        bgcolor = c;
    }
    
//*****************************************************************************
	
    /**
     * Returns the Background color.
     * @return The current background color
     */
    public Color getBackground() {
        return bgcolor;
    }
	
//*****************************************************************************
    
    /**
     * The setOn method
     */
    public void setGrid(boolean is_on) {
        grid = is_on;
    }
    
//*****************************************************************************
	
    /**
     * The getOn method
     */
    public boolean isGridOn() {
        return grid;
    }
    
//*****************************************************************************
	
    /**
     * The setInc method
     */
    public void setXYInc(double x, double y) {
        xInc = x;
        yInc = y;
    }
    
//*****************************************************************************
	
    /**
     * The getXInc method
     */
    public double getXInc() {
        return xInc;
    }
    
//*****************************************************************************
	
    /**
     * The getYInc method
     */
    public double getYInc() {
        return yInc;
    }
    
//*****************************************************************************
	
    /**
     * The setSnap method
     */
    public void setSnap(boolean b) {
        snap = b;
    }
    
//*****************************************************************************
	
    /**
     * The getSnap method
     */
    public boolean getSnap() {
        return snap;
    }
    
//*****************************************************************************
	
    /**
     * The setViewGlobalAxis method
     */
    public void setViewGlobalAxis(boolean b) {
        viewGlobalAxis = b;
    }
    
//*****************************************************************************
	
    /**
     * The getViewGlobalAxis method
     */
    public boolean getViewGlobalAxis() {
        return viewGlobalAxis;
    }
	
//*****************************************************************************
	
    /**
     * The setViewLocalAxis method
     */
    public void setViewLocalAxis(boolean b) {
        viewLocalAxis = b;
    }
    
//*****************************************************************************
	
    /**
     * The getViewLocalAxis method
     */
    public boolean getViewLocalAxis() {
        return viewLocalAxis;
    }
	
//*****************************************************************************
	
    /**
     * The setViewNodeLabel method
     */
    public void setViewNodeLabel(boolean b) {
        viewNodeLabel = b;
    }
    
//*****************************************************************************
	
    /**
     * The getViewNodeLabel method
     */
    public boolean getViewNodeLabel() {
        return viewNodeLabel;
    }
	
//*****************************************************************************
	
	/**
	 * Sets the viewElementLabel variable.
	 * @param  sl The desired variable to be set.
	 */
	public void setViewElementLabel(boolean sl) {
		viewElementLabel = sl;
	}
	
//*****************************************************************************
	
	/**
	 * Returns the viewElementLabel variable.
	 * @return The current viewElementLabel value.
	 */
	public boolean getViewElementLabel() {
		return viewElementLabel;
	}
	
//*****************************************************************************
	
    /**
     * The setViewRegionLabel method
     */
    public void setViewRegionLabel(boolean b) {
        viewRegionLabel = b;
    }
    
//*****************************************************************************
	
    /**
     * The getViewRegionLabel method
     */
    public boolean getViewRegionLabel() {
        return viewRegionLabel;
    }
	
//*****************************************************************************

	public void setActualTreeNode(int a) {
		actualTreeNode = a;
	}
	
//*****************************************************************************
	
	public int getActualTreeNode() {
		return actualTreeNode;
	}
	
//*****************************************************************************
	
	/**
	 * Appends the specified element color to the end of this list.
	 * @param  b The element color to be inserted.
	 */
	public void addColor(Color c) {
		colors.add(c);
	}
	
//*****************************************************************************
	
	/**
	 * Returns the element color at the specified position in this list.
	 * @return The element color at the specified position in this list.
	 */
	public Color getColor(int index) {
		while ( colors.size()<=index ) {
			float r = (float)Math.random();
			if (r < 0.5) r = r + (float)0.5;
			float g = (float)Math.random();
			if (g < 0.5) g = g + (float)0.5;
			float b = (float)Math.random();
			if (b < 0.5) b = b + (float)0.5;
			colors.add( new Color(r, g, b) );
		}
		return (Color)colors.get(index);
	}
	
//*****************************************************************************
	
	/**
	 * Removes all of the elements color from this list.
	 * The list will be empty after this call returns.
	 */
	public void clearColor() {
		colors.clear();
	} // End of clearColor
	
//*****************************************************************************
}
